package com.spark.trouble

import android.annotation.SuppressLint
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.ext.alert
import com.spark.common.ext.report
import com.spark.common.ext.wrapIO
import com.spark.common.net.Http
import com.spark.common.net.toJsonBody
import com.spark.trouble.model.HTHistoryModel
import kotlinx.android.synthetic.main.frag_ht_history.*

/**
 * 隐患历史
 */
class HTHistoryFragment : BaseFragment() {
    private var currPage = 1
    private var totalPage = 1
    private lateinit var htAdapter: HtAdapter

    companion object {
        fun newInstance(): HTHistoryFragment {
            val target = HTHistoryFragment()
            val args = Bundle()

            target.arguments = args
            return target
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_ht_history
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        iv_back.setOnClickListener { pop() }
        tv_title.text = "隐患历史"

        htAdapter = HtAdapter(R.layout.item_ht_history).apply {
            setOnItemClickListener { adapter, view, position ->
                activity?.apply {
                    val data = htAdapter.data[position]
                    HTHistoryDetailActivity.actionStart(this, Gson().toJson(data))
                }
            }
        }
        recy_hts.apply {
            adapter = htAdapter
            layoutManager = LinearLayoutManager(context)
        }
        refresh_layout.apply {
            setOnRefreshListener {
                currPage = 1
                fetchHtHistorys(currPage)
            }
            setOnLoadmoreListener {
                if (currPage >= totalPage) {
                    alert("没有更多数据了")
                } else {
                    currPage++
                    fetchHtHistorys(currPage)
                }
            }
        }
        iv_category.setOnClickListener {
            val diaHTFilterFragment = DiaHTFilterFragment.newInstance()
            diaHTFilterFragment.addFilterListener {

            }
            diaHTFilterFragment.show(childFragmentManager, DiaHTFilterFragment.javaClass.canonicalName)
        }
        fetchHtHistorys(currPage)
        loading()
    }

    @SuppressLint("CheckResult")
    private fun fetchHtHistorys(page: Int) {
        /**
         * 其他参数
         * searchstr: 子任务Id
         */
        val params = mutableMapOf(
                Pair(Const.qr_page, "$page"),
                Pair(Const.qr_pageSize, "10")
        )
        Http.request()
                .rxPost("${Const.base_url}${Const.act_ht_history}", params.toJsonBody())
                .wrapIO()
                .subscribe(
                        {
                            Gson().fromJson(it, HTHistoryModel::class.java)?.apply {
                                if (code == "200") {
                                    if (page == 1) {
                                        htAdapter.data.clear()
                                    }
                                    htAdapter.addData(this.resultMaps[0].page.results)
                                }
                            }
                        },
                        {
                            it.printStackTrace()
                            report(it)
                            hideLoader()
                            if (page == 1) refresh_layout.finishRefresh() else refresh_layout.finishLoadmore()
                        },
                        {
                            hideLoader()
                            if (page == 1) refresh_layout.finishRefresh() else refresh_layout.finishLoadmore()
                        },
                        {
                            dispose = it
                        }
                )
    }
}

class HtAdapter(layoutRes: Int) : BaseQuickAdapter<HTHistoryModel.ResultMap.Page.Result, BaseViewHolder>(layoutRes) {
    override fun convert(helper: BaseViewHolder?, item: HTHistoryModel.ResultMap.Page.Result?) {
        if (item == null) return
        helper?.apply {
            setText(R.id.tv_ht_name, item.sourceDescription)
            setText(R.id.tv_ht_content, item.troubleContent)
            setText(R.id.tv_state, item.state)

        }
    }

}
